<?php
namespace app\wechat\controller;

use app\common\controller\AdminBaseController;
use app\wechat\model\WechatMessage as MessageModel;
/**
 * 微信群发
 */
class Message extends AdminBaseController {

	protected $message_model;

	protected function _initialize() {
		parent::_initialize();
		$this -> message_model = new MessageModel();
	}

	/**
	 * 后台菜单
	 * @return mixed
	 */
	public function index() {
		if ($this -> request -> isPost()) {
			$data = $this -> request -> post();
			$receive = load_wechat('Receive');
			$is_to_all=true;//是否群发
			$tg_id=0;//分组id
			if($data['label']!=""){
				$is_to_all=false;
				$tg_id=$data['label'];
			}
			$filter_data=array('is_to_all'=>$is_to_all,'tag_id'=>$tg_id);
			$mpnews_data=array('media_id'=>$data['media_id']);
			$send_data['filter'] = $filter_data;
			$send_data['mpnews'] = $mpnews_data;
			$send_data['msgtype'] = 'mpnews';
			$send_res = $receive -> sendGroupMassMessage($send_data);
			if($send_res !== false){
				$data['add_time'] = now_time();
				$data['message']=htmlspecialchars_decode($data['message']);
				$data['response']=$send_res;
				if ($this -> message_model -> allowField(true) -> save($data)) {
					$this -> success('发送成功');
				} else {
					$this -> error('发送失败');
				}
			}else{
				$this -> error('发送失败');
			}
			
		} else {
			$wechat = load_wechat('User');
			$label=$wechat->getTags();
			$news=db("wechat_media")->where('type','news')->select();
			$this->assign("label",$label['tags']);
			$this->assign("news",$news);
			return $this -> fetch();
		}
	}
	/**
	 * 群发历史
	 */
	public function getlist(){
		
	}

}
